<template>
  <view class="container">
    <view class="header">
      <view class="title">公式速判设置</view>
      <view class="subtitle">根据公式生成算式选择正确答案</view>
    </view>

    <view class="settings-container">
      <!-- 公式类型选择 -->
      <view class="setting-section">
        <view class="section-title">
          <uni-icons color="#007AFF" size="20" type="list"></uni-icons>
          <text>公式类型</text>
        </view>
        <view class="option-grid">
          <view
              v-for="formula in formulaOptions"
              :key="formula.key"
              :class="{ 'selected': selectedFormulas.includes(formula.key) }"
              class="option-item"
              @tap="toggleFormula(formula.key)"
          >
            <view class="option-icon">{{ formula.symbol }}</view>
            <text class="option-text">{{ formula.name }}</text>
          </view>
        </view>
      </view>

      <!-- 题目数量设置 -->
      <view class="setting-item">
        <view class="setting-label">
          <uni-icons color="#007AFF" size="20" type="list"></uni-icons>
          <text>题目数量</text>
        </view>
        <view class="setting-value">
          <uni-number-box
              :max="20"
              :min="5"
              :value="questionCount"
              @change="onQuestionCountChange"
          ></uni-number-box>
        </view>
      </view>

      <!-- 选项数量设置 -->
      <view class="setting-item">
        <view class="setting-label">
          <uni-icons color="#007AFF" size="20" type="bars"></uni-icons>
          <text>每题选项数</text>
        </view>
        <view class="setting-value">
          <uni-number-box
              :max="6"
              :min="3"
              :value="optionsCount"
              @change="onOptionsCountChange"
          ></uni-number-box>
        </view>
      </view>

      <!-- 历史最佳 -->
      <view v-if="bestRecord" class="stats-card">
        <view class="stats-title">历史最佳</view>
        <view class="stats-content">
          <view class="stat-item">
            <text class="stat-value">{{ bestRecord.score }}分</text>
            <text class="stat-label">最高分数</text>
          </view>
          <view class="stat-item">
            <text class="stat-value">{{ bestRecord.accuracy }}%</text>
            <text class="stat-label">最高正确率</text>
          </view>
        </view>
      </view>

      <!-- 练习说明 -->
      <view class="info-card">
        <view class="info-title">
          <uni-icons color="#28a745" size="20" type="info"></uni-icons>
          <text>练习说明</text>
        </view>
        <view class="info-content">
          <text>根据给定的公式和参数，计算算式的正确答案，并从选项中选择最接近的答案。</text>
        </view>
      </view>
    </view>

    <!-- 开始按钮 -->
    <view class="start-container">
      <button
          :class="{ 'disabled': !canStart }"
          :disabled="!canStart"
          class="start-btn"
          @tap="startPractice"
      >
        <uni-icons color="#ffffff" size="20" type="right"></uni-icons>
        <text>开始练习</text>
      </button>
    </view>
  </view>
</template>

<script>
import Storage from '@/utils/storage.js';

export default {
  data() {
    return {
      selectedFormulas: ['formula1', 'formula2'],
      questionCount: 10,
      optionsCount: 4,
      bestRecord: null,
      formulaOptions: [
        {key: 'formula1', name: '求基期量', symbol: 'A1'},
        {key: 'formula2', name: '求增长率', symbol: 'A2'},
        {key: 'formula3', name: '求基期比重', symbol: 'A3'},
        {key: 'formula4', name: '求两期比重差', symbol: 'A4'},
        {key: 'formula5', name: '年均增长求现期量', symbol: 'A5'}
      ]
    }
  },
  computed: {
    canStart() {
      return this.selectedFormulas.length > 0 &&
          this.questionCount >= 5;
    }
  },
  onLoad() {
    this.loadSettings();
    this.loadBestRecord();
  },
  methods: {
    // 切换公式类型
    toggleFormula(formula) {
      const index = this.selectedFormulas.indexOf(formula);
      if (index > -1) {
        if (this.selectedFormulas.length > 1) {
          this.selectedFormulas.splice(index, 1);
        }
      } else {
        this.selectedFormulas.push(formula);
      }
      this.saveSettings();
    },

    // 题目数量变化
    onQuestionCountChange(value) {
      this.questionCount = value;
      this.saveSettings();
    },

    // 选项数量变化
    onOptionsCountChange(value) {
      this.optionsCount = value;
      this.saveSettings();
    },

    // 开始练习
    startPractice() {
      if (!this.canStart) {
        uni.showToast({
          title: '请选择练习设置',
          icon: 'none'
        });
        return;
      }

      const settings = {
        formulas: this.selectedFormulas,
        count: this.questionCount,
        options: this.optionsCount
      };

      uni.navigateTo({
        url: `/pages/speed-formula-choice/practice?settings=${encodeURIComponent(JSON.stringify(settings))}`
      });
    },

    // 加载设置
    loadSettings() {
      const settings = Storage.getUserSettings();
      this.selectedFormulas = settings.speedFormulaChoiceFormulas || ['formula1', 'formula2'];
      this.questionCount = settings.speedFormulaChoiceQuestionCount || 10;
      this.optionsCount = settings.speedFormulaChoiceOptionsCount || 4;
    },

    // 保存设置
    saveSettings() {
      Storage.saveUserSettings({
        speedFormulaChoiceFormulas: this.selectedFormulas,
        speedFormulaChoiceQuestionCount: this.questionCount,
        speedFormulaChoiceOptionsCount: this.optionsCount
      });
    },

    // 加载最佳记录
    loadBestRecord() {
      try {
        const records = Storage.get('speed_formula_choice_records', []);
        if (records.length > 0) {
          const bestScore = Math.max(...records.map(r => r.score || 0));
          const bestAccuracy = Math.max(...records.map(r => r.accuracy || 0));

          this.bestRecord = {
            score: bestScore,
            accuracy: bestAccuracy
          };
        }
      } catch (e) {
        console.log('Failed to load best record:', e);
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding-bottom: 120rpx;
}

.header {
  background: linear-gradient(135deg, #8a2be2, #9370db);
  padding: 60rpx 40rpx 40rpx;
  color: white;
  text-align: center;

  .title {
    font-size: 40rpx;
    font-weight: bold;
    margin-bottom: 15rpx;
  }

  .subtitle {
    font-size: 26rpx;
    opacity: 0.9;
  }
}

.settings-container {
  padding: 40rpx;
}

.setting-section {
  background: white;
  border-radius: 15rpx;
  padding: 30rpx;
  margin-bottom: 30rpx;
  box-shadow: 0 5rpx 15rpx rgba(0, 0, 0, 0.05);

  .section-title {
    display: flex;
    align-items: center;
    margin-bottom: 25rpx;

    text {
      font-size: 30rpx;
      font-weight: bold;
      margin-left: 10rpx;
    }
  }

  .option-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 20rpx;
  }

  .option-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20rpx 10rpx;
    border: 2rpx solid #e0e0e0;
    border-radius: 10rpx;
    transition: all 0.2s;

    &.selected {
      border-color: #007AFF;
      background-color: rgba(0, 122, 255, 0.1);
    }

    .option-icon {
      width: 50rpx;
      height: 50rpx;
      background: linear-gradient(135deg, #007AFF, #0a6dcf);
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      color: white;
      font-weight: bold;
      margin-bottom: 10rpx;
    }

    .option-text {
      font-size: 24rpx;
      text-align: center;
    }
  }
}

.setting-item {
  background: white;
  border-radius: 15rpx;
  padding: 30rpx;
  margin-bottom: 30rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 5rpx 15rpx rgba(0, 0, 0, 0.05);

  .setting-label {
    display: flex;
    align-items: center;

    text {
      font-size: 30rpx;
      margin-left: 10rpx;
    }
  }

  .setting-value {
    display: flex;
    align-items: center;
  }
}

.stats-card {
  background: white;
  border-radius: 15rpx;
  padding: 30rpx;
  margin-bottom: 30rpx;
  box-shadow: 0 5rpx 15rpx rgba(0, 0, 0, 0.05);

  .stats-title {
    font-size: 30rpx;
    font-weight: bold;
    margin-bottom: 20rpx;
    color: #333;
  }

  .stats-content {
    display: flex;
    justify-content: space-around;

    .stat-item {
      text-align: center;

      .stat-value {
        display: block;
        font-size: 36rpx;
        font-weight: bold;
        color: #007AFF;
        margin-bottom: 10rpx;
      }

      .stat-label {
        font-size: 24rpx;
        color: #666;
      }
    }
  }
}

.info-card {
  background: white;
  border-radius: 15rpx;
  padding: 30rpx;
  box-shadow: 0 5rpx 15rpx rgba(0, 0, 0, 0.05);

  .info-title {
    display: flex;
    align-items: center;
    margin-bottom: 20rpx;

    text {
      font-size: 30rpx;
      font-weight: bold;
      margin-left: 10rpx;
      color: #333;
    }
  }

  .info-content {
    font-size: 26rpx;
    color: #666;
    line-height: 1.6;
  }
}

.start-container {
  position: sticky;
  bottom: 0;
  padding: 40rpx;
  background: white;

  .start-btn {
    width: 100%;
    height: 80rpx;
    background: linear-gradient(135deg, #007AFF, #0a6dcf);
    border-radius: 40rpx;
    color: white;
    font-size: 32rpx;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;

    &.disabled {
      background: #ccc;
    }

    text {
      margin-left: 10rpx;
    }
  }
}
</style>